//
//  KHUICVHeader_Home.m
//  EduCourse
//
//  Created by yanyan on 2017/8/9.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUICVHeader_Home.h"

@interface ECUICVHeader_Home()<CYTabbarViewDatasource>

@end
@implementation ECUICVHeader_Home
@synthesize lineView = _lineView;
@synthesize bottomView = _bottomView;
-(void)addSubviews
{
    [super addSubviews];
    [self addSubview:self.selectedButton];
    [self addSubview:self.interestCategoryView];
    [self addSubview:self.lineView];
    [self addSubview:self.bottomView];
}
- (UIView*)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(99, 0, 0.5,self.height-0.5)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"c2c2c2"];
    }
    return _lineView;
}
-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 0.5, CGRectGetWidth(self.bounds),0.5)];
        _bottomView.backgroundColor = [UIColor colorWithHexString:@"c2c2c2"];
    }
    return _bottomView;
}
- (CYUIView_Tabbar *)interestCategoryView
{
    
    if (!_interestCategoryView) {
        WeakSelf(weakSelf);
        _interestCategoryView = [[CYUIView_Tabbar alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_selectedButton.frame), CGRectGetMinY(self.selectedButton.frame), kScreenWidth - CGRectGetMaxX(_selectedButton.frame), 40)];
        _interestCategoryView.defaultSelectedIndex = 0;
        _interestCategoryView.backgroundColor = [UIColor clearColor];
        _interestCategoryView.locationType = CYTabbarIndicatorLocationDown;
        _interestCategoryView.indicatorAttributes = @{CYTabIndicatorColor:ECTHEMESELECTEDCOLOR,CYTabIndicatorViewHeight:@(5),CYTabBoxBackgroundColor:[UIColor colorWithHexString:@"#000000"],CYTabIndicatorCornerRadius:@(2.5),CYTabIndicatorViewWidth:@(5)};
        _interestCategoryView.normalAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:SELECTEDBUTTONFONT_NORMALSIZE],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#737573"]};
        _interestCategoryView.selectedAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:SELECTEDBUTTONFONT_SELECTEDSIZE],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#000000"]};
        _interestCategoryView.selectionType = CYTabbarSelectionIndicator;
        _interestCategoryView.indicatorEqualTitleWidth = NO;
        _interestCategoryView.bottomView.frame = CGRectMake(0, 0, 5, 5);
        _interestCategoryView.scrollEnable = YES;
        _interestCategoryView.dataSource = self;
        
        _interestCategoryView.block_IndexPathSelected = ^(NSIndexPath * _Nonnull indexPath) {
            //                [weakSelf.scrollView setContentOffset:CGPointMake(kScreenWidth*indexPath.row, 0) animated:YES];
            if (weakSelf.block_objectAtIndex) {
                weakSelf.block_objectAtIndex(weakSelf.interestCategory.subCategoryArray[indexPath.row],indexPath.row);
            }
        };
        
    }
    
    return _interestCategoryView;
}
- (UIButton *)selectedButton
{
    if (!_selectedButton) {
        _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectedButton setTitle:@"推荐" forState:UIControlStateNormal];
        [_selectedButton setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
        [_selectedButton setImage:[UIImage imageNamed:@"selectedButton_Normal"] forState:UIControlStateNormal];
        _selectedButton.titleLabel.font = [UIFont systemFontOfSize:14 * kPercentage];
        _selectedButton.titleLabel.adjustsFontSizeToFitWidth = YES;
        CGSize size = [[_selectedButton titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _selectedButton.titleLabel.font}];
        _selectedButton.frame = CGRectMake(0, kProportion_Height(150)/2.0 - 20, 100, 40);
        _selectedButton.titleEdgeInsets = UIEdgeInsetsMake(0, -13*kPercentage, 0, 13 * kPercentage);
        _selectedButton.imageEdgeInsets = UIEdgeInsetsMake((40 - 7 *kPercentage)/2, size.width+5 , (40 - 7 *kPercentage)/2, -size.width-5);
        [_selectedButton addTarget:self action:@selector(action_showCategory:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectedButton;
}
- (NSArray<NSString *> *)tabbarTitlesForTabbarView:(CYUIView_Tabbar *)tabbarView
{
    if (_interestCategoryTitleArray) {
        return _interestCategoryTitleArray;
    }
    return @[];
}
- (void)setInterestCategory:(PrimaryCategoryModel *)interestCategory
{
    _interestCategory = interestCategory;
    [self.selectedButton setTitle:interestCategory.title forState:UIControlStateNormal];
    CGSize size = [[_selectedButton titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _selectedButton.titleLabel.font}];
    size = CGSizeMake(MIN(size.width, 80),  MIN(size.height, 40));
    _selectedButton.titleEdgeInsets = UIEdgeInsetsMake(0, -13*kPercentage, 0, 13 * kPercentage);
    _selectedButton.imageEdgeInsets = UIEdgeInsetsMake((40 - 7 *kPercentage)/2, size.width+5 , (40 - 7 *kPercentage)/2, -size.width-5);
    if (!_interestCategoryTitleArray) {
        _interestCategoryTitleArray = [NSMutableArray array];
    }
    [_interestCategoryTitleArray removeAllObjects];
    for (PrimaryCategoryModel *mo in _interestCategory.subCategoryArray) {
        [_interestCategoryTitleArray addObject:mo.title];
    }
    _interestCategoryView.tabbarOffsetX = 0;
    [_interestCategoryView reloadData];
}
- (void)setFloating:(BOOL)floating
{
    _floating = floating;
    if (floating) {
        [self.selectedButton setImage:[UIImage imageNamed:@"selectedButton_Top"] forState:UIControlStateNormal];
        [self.selectedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.interestCategoryView.indicatorAttributes = @{CYTabIndicatorColor:[UIColor whiteColor],CYTabBoxBackgroundColor:[UIColor colorWithHexString:@"#000000"]};
        self.interestCategoryView.normalAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHex:0xffffff alpha:0.6]};
        self.interestCategoryView.selectedAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#ffffff"]};
        self.lineView.backgroundColor = ECTHEMESELECTEDCOLOR;
        self.bottomView.backgroundColor = ECTHEMESELECTEDCOLOR;
        [self setBackgroundColor:ECTHEMESELECTEDCOLOR];
        
    } else {
        [self.selectedButton setImage:[UIImage imageNamed:@"selectedButton_Normal"] forState:UIControlStateNormal];
        [self.selectedButton setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
        self.interestCategoryView.indicatorAttributes = @{CYTabIndicatorColor:ECTHEMESELECTEDCOLOR,CYTabIndicatorViewHeight:@(5),CYTabBoxBackgroundColor:[UIColor colorWithHexString:@"#000000"],CYTabIndicatorCornerRadius:@(2.5),CYTabIndicatorViewWidth:@(5)};
        self.interestCategoryView.normalAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:SELECTEDBUTTONFONT_NORMALSIZE],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#737573"]};
        self.interestCategoryView.selectedAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:SELECTEDBUTTONFONT_SELECTEDSIZE],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#000000"]};
        self.lineView.backgroundColor = [UIColor colorWithHexString:@"#c2c2c2"];
        self.bottomView.backgroundColor = [UIColor colorWithHexString:@"#c2c2c2"];
        [self setBackgroundColor:[UIColor whiteColor]];
        
    }
        [self.interestCategoryView setTabIndex:self.interestCategoryView.selectedIndex animated:NO];
        [self.interestCategoryView reloadData];

}
- (void)action_showCategory:(id)sender{
    if (self.block_selected) {
        self.block_selected(sender);
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
